package Java练习.字符串练习;

import java.util.Scanner;
import java.util.StringJoiner;

public class 转换罗马数字 {
    public static void main(String[] args) {

        //获取键盘输入
        Scanner sc = new Scanner(System.in);
        String str=null;
        for (int i=0;i<1;){
            System.out.print("请输入您要转换的数字：");
            str=sc.next();
            //判断输入是否是数字
            if(!isnum(str)){
                System.out.println("输入的不是纯数字,输入不合法!");
                continue;
            }
            if (str.length()<=9&&str.length()>0){
                i++;
            }else{
                System.out.println("输入不和法!");
            }
        }
        //转换字符串数字为罗马数字
        StringJoiner sj=new StringJoiner("-");
        for (int i = 0; i < str.length(); i++) {
            //转换对应的char为对应的数字
            int num=str.charAt(i)-'0';
            sj.add(covert(num)).toString();
        }
        System.out.println(sj);


        }
    //定义一个方法判断是否是数字
    public static boolean isnum(String num){
        for (int i = 0; i < num.length(); i++) {
            char ch=num.charAt(i);
            if (!((ch-'0'>=0)&&(ch-'0')<=9)){
                return false;
            }
        }
        return true;
    }
    //定义一个方法返回对应的罗马数字
    public  static String covert(int number){
        //创建一个罗马数字数组
        String[] str_num={"","Ⅰ","Ⅱ","Ⅲ","Ⅳ","Ⅴ","Ⅵ","Ⅶ","Ⅷ","Ⅸ"};
        return str_num[number];
    }
}
